l = [5, 10, 5, 10, 10, 20, 20, 5, 10]

def func(l):
    i = 0
    for h in l:
        if h == 5:
            i += 1

    return i

# 5, 5, 10, 20

def fun(l):
    if l[0] != 5:
        return False

    q = -1

    for i in l:
        q += 1       # i5 q0     i5 q1    i10 q2      i20 q2
        if i == 10:
            if 5 in l[0:q]:
                l[0:q].remove(5)    # 5 10 20
                q -= 1           # q1
            else:
                return False

        elif i == 20:
            if ((5 in l[0:q]) and (10 in l[0:q])):
                l[0:q].remove(5)
                l[0:q].remove(10)
                q -= 2

            elif func(l[0:q]) >= 3:
                l[0:q].remove(5)
                l[0:q].remove(5)
                l[0:q].remove(5)
                q -= 3

            else:
                return False
    return True


print(fun([5, 5, 10, 20]))